<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>中医药科普短视频生成系统</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background-color: white;
            border-radius: 10px;
            padding: 30px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        }
        h1 {
            text-align: center;
            color: #2c3e50;
            margin-bottom: 30px;
        }
        .input-group {
            display: flex;
            margin-bottom: 20px;
        }
        #herbName {
            flex: 1;
            padding: 12px;
            border: 2px solid #3498db;
            border-radius: 5px 0 0 5px;
            font-size: 16px;
        }
        #generateBtn {
            padding: 12px 24px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 0 5px 5px 0;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        #generateBtn:hover {
            background-color: #2980b9;
        }
        #result {
            margin-top: 30px;
            padding: 20px;
            border-radius: 5px;
            background-color: #ecf0f1;
            display: none;
        }
        #error {
            color: #e74c3c;
            font-weight: bold;
            text-align: center;
            margin-top: 20px;
            display: none;
        }
        .video-title {
            font-size: 24px;
            color: #2c3e50;
            margin-bottom: 15px;
            text-align: center;
        }
        .script-section {
            margin-bottom: 20px;
        }
        .script-line {
            margin: 10px 0;
            padding: 8px;
            background-color: #fff;
            border-left: 4px solid #3498db;
        }
        .facts-section {
            margin-bottom: 20px;
        }
        .fact-item {
            margin: 10px 0;
            padding: 8px;
            background-color: #fff;
        }
        .loading {
            text-align: center;
            display: none;
        }
        .visual-section {
            margin-bottom: 20px;
        }
        .visual-item {
            margin: 15px 0;
            padding: 10px;
            background-color: #fff;
            border: 1px solid #ddd;
        }
        .audio-section {
            padding: 10px;
            background-color: #fff;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>中医药科普短视频生成系统</h1>
        <div class="input-group">
            <input type="text" id="herbName" placeholder="请输入中草药名称，如：黄芪">
            <button id="generateBtn">生成短视频内容</button>
        </div>
        <div id="error"></div>
        <div class="loading" id="loading">正在生成中...</div>
        <div id="result">
            <div class="video-title" id="videoTitle"></div>
            
            <div class="script-section">
                <h3>视频脚本</h3>
                <div id="scriptContent"></div>
            </div>
            
            <div class="facts-section">
                <h3>权威知识点</h3>
                <div id="factsContent"></div>
            </div>
            
            <div class="visual-section">
                <h3>画面建议</h3>
                <div id="visualContent"></div>
            </div>
            
            <div class="audio-section">
                <h3>音频建议</h3>
                <p><strong>背景音乐：</strong><span id="bgm"></span></p>
                <p><strong>音效：</strong><span id="sfx"></span></p>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('generateBtn').addEventListener('click', generateVideoContent);
        document.getElementById('herbName').addEventListener('keypress', function(e) {
            if (e.key === 'Enter') {
                generateVideoContent();
            }
        });

        function generateVideoContent() {
            const herbName = document.getElementById('herbName').value.trim();
            if (!herbName) {
                showError('请输入中草药名称');
                return;
            }

            // 显示加载状态
            document.getElementById('loading').style.display = 'block';
            document.getElementById('result').style.display = 'none';
            document.getElementById('error').style.display = 'none';

            // 调用后端API
            fetch(`/herbvideo/${encodeURIComponent(herbName)}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('loading').style.display = 'none';
                    
                    if (data.error) {
                        showError(data.error);
                    } else {
                        displayResult(data);
                    }
                })
                .catch(error => {
                    document.getElementById('loading').style.display = 'none';
                    showError('生成失败，请稍后重试');
                    console.error('Error:', error);
                });
        }

        function displayResult(data) {
            // 显示标题
            document.getElementById('videoTitle').textContent = data.title;

            // 显示脚本
            const scriptContent = document.getElementById('scriptContent');
            scriptContent.innerHTML = '';
            data.script.forEach(line => {
                const div = document.createElement('div');
                div.className = 'script-line';
                div.textContent = line;
                scriptContent.appendChild(div);
            });

            // 显示知识点
            const factsContent = document.getElementById('factsContent');
            factsContent.innerHTML = '';
            data.keyFacts.forEach(fact => {
                const div = document.createElement('div');
                div.className = 'fact-item';
                div.textContent = fact;
                factsContent.appendChild(div);
            });

            // 显示画面建议
            const visualContent = document.getElementById('visualContent');
            visualContent.innerHTML = '';
            data.visualSuggestions.forEach(item => {
                const div = document.createElement('div');
                div.className = 'visual-item';
                div.innerHTML = `
                    <p><strong>文案：</strong>${item.text}</p>
                    <p><strong>场景：</strong>${item.scene}</p>
                    <p><strong>效果：</strong>${item.effect}</p>
                `;
                visualContent.appendChild(div);
            });

            // 显示音频建议
            document.getElementById('bgm').textContent = data.audio.bgm;
            document.getElementById('sfx').textContent = data.audio.sfx.join(', ');

            // 显示结果
            document.getElementById('result').style.display = 'block';
        }

        function showError(message) {
            const errorElement = document.getElementById('error');
            errorElement.textContent = message;
            errorElement.style.display = 'block';
        }
    </script>
</body>
</html>